package com.jinbo.footmessage.serverInf;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.ProjectBean;

import org.w3c.dom.Text;

import java.util.List;


/**
 * Created by jinbo on 2015/8/19.
 */
public class ProjectItemListViewAdapter extends BaseAdapter implements ShopItemListViewAdapter.Callbacks {
    private TextView mNumTv;
    private TextView mPriceTv;
    List<ProjectBean> projects;
  /*  private Callbacks mCallBacks;
    public interface Callbacks
    {
        public void change(int number);
    }*/

    static class ViewHolder {
        //LinearLayout ll;
        ImageView buyIv;
        //TextView test;

        TextView tv_name,tv_duration,tv_price;
    }

    //ArrayList<ProjectsBean> projectdatalist;
    private Activity context;

    public ProjectItemListViewAdapter(Activity context,List<ProjectBean> datalist) {//ArrayList<ProjectsBean> datalist) {
        this.context = context;
        //mCallBacks=(Callbacks)context;
        mNumTv =(TextView)context.findViewById(R.id.shop_num);
        mPriceTv =(TextView)context.findViewById(R.id.shop_price);
        projects=datalist;

    }

    void setprojects( List<ProjectBean> projectdatalist)
    {
        this.projects=projectdatalist;
    }

    List<ProjectBean> getProjects()
    {
        return  projects;
    }

    @Override
    public int getCount() {
            // TODO Auto-generated method stub*//*
            if (projects!= null) {
                return projects.size();
            }
        return 0;
    }

    public int getTime()
    {
        int sum_time=0;
        for(int i=0;i<projects.size();i++){
            if(projects.get(i).flag)
                sum_time=sum_time+Integer.valueOf(projects.get(i).duration);
        }
        return  sum_time;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
            if (projects != null) {
                return projects.get(position);
            }
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    ViewHolder holder = null;
   // boolean flag = false;

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_project_item, null);
            holder = new ViewHolder();
            holder.buyIv = (ImageView) convertView.findViewById(R.id.buy);
            holder.tv_name=(TextView)convertView.findViewById(R.id.name);
            holder.tv_duration=(TextView)convertView.findViewById(R.id.duration);
            holder.tv_price=(TextView)convertView.findViewById(R.id.price);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (projects.get(position).flag)
            holder.buyIv.setImageResource(R.drawable.shop_icon);
        else
            holder.buyIv.setImageResource(R.drawable.shop_icon_no);
        holder.tv_name.setText(projects.get(position).name);
        holder.tv_duration.setText(projects.get(position).duration+"分钟");
        holder.tv_price.setText(projects.get(position).price+"元");
        holder.buyIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!projects.get(position).flag) {
                    projects.get(position).flag = true;
                    // flag = true;
                    // mCallBacks.change(6);
                    //f(6);
                    add();
                } else {
                    projects.get(position).flag = false;
                    //flag = false;
                    // mCallBacks.change(0);
                    //  f(0);
                    cut();
                }

                notifyDataSetChanged();

            }
        });
        return convertView;
    }

 /*   void f(int num)
    {
       mNumTv.setText("" + num);
    }*/
    int num=0;
    void add()
    {
        num++;
        mNumTv.setText(""+(num));
        updatePrice();

    }

    void updatePrice()
    {
        int sum_price=0;
        for(int i=0;i<projects.size();i++){
            if(projects.get(i).flag)
                sum_price=sum_price+Integer.valueOf(projects.get(i).price);
        }
        mPriceTv.setText(sum_price + "");
    }
    void cut()
    {
        num--;
        mNumTv.setText("" + (num));
        updatePrice();
    }

    @Override
    public void change(List<ProjectBean> projects) {
        // TODO Auto-generated method stub
        setprojects(projects);
        notifyDataSetChanged();
        int sum_price=0;
        int sum_num=0;
        for(int i=0;i<projects.size();i++){
            if(projects.get(i).flag) {
                sum_price = sum_price + Integer.valueOf(projects.get(i).price);
                sum_num++;
            }
        }
        mPriceTv.setText(sum_price + "");
        num=sum_num;
        mNumTv.setText("" + sum_num);
    }
}
